package com.itcast.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.client.RestTemplate;
import com.itcast.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/pages")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/findOrder/{id}")
    public Goods finOrder( @PathVariable("id") int id){
//动态获取Eureka中的provider中的访问路径
        List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");
        //list没数据
        if(instances==null||instances.size()==0){
            return null;
        }
        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();
        int port = instance.getPort();

        String url="http://"+host+":"+port+"/pages/findById/"+id;
        //远程调用服务提供者
        return restTemplate.getForObject(url,Goods.class );
    }

}
